package com.wing.cabinet.controller.app;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.cabinet.model.entity.Cabinet;
import com.wing.cabinet.model.entity.StorageInventoryInout;
import com.wing.cabinet.model.entity.StorageInventorySpace;
import com.wing.cabinet.model.response.CellSingleInventoryProductVO;
import com.wing.cabinet.service.CellSingleInventoryProductService;
import com.wing.common.utils.JsonResult;
import com.wing.web.utils.LocaleMessage;
import com.wing.common.utils.PageResult;
import com.wing.cabinet.model.entity.CabinetCargoSpace;
import com.wing.cabinet.model.response.CabinetCargoSpaceVO;
import com.wing.cabinet.service.CabinetCargoSpaceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wing.cabinet.model.request.CabinetCargoSpaceForm;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-收刀柜货位管理", tags = "收刀柜货位管理")
@RestController("appCabinetCargoSpaceController")
@RequestMapping("cabinet/app/cabinetCargoSpace")
public class CabinetCargoSpaceController {

    @Autowired
    private CabinetCargoSpaceService cabinetCargoSpaceService;
    @Autowired
    private LocaleMessage localeMessage;

    @Autowired
    private CellSingleInventoryProductService cellSingleInventoryProductService;

    @ApiOperation(value = "柜子库位列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "cabCode", value = "柜子编码", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<CabinetCargoSpaceVO> list(
            @RequestParam(name = "page",defaultValue = "1") Integer page
            , @RequestParam(name = "limit",defaultValue = "10") Integer limit
            , @RequestParam(name = "sort",defaultValue = "cast(t.code as unsigned)") String sort
            , @RequestParam(name = "order",defaultValue = "asc") String order
            , @RequestParam(name = "queryFlag",required = false) String queryFlag
            , String cabCode
            , Long cabId
            , StorageInventorySpace.BusinessType businessType
            ) {
        Page<CabinetCargoSpaceVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        params.put("cabinetId",cabId);
        params.put("cabCode",cabCode);
        if (businessType!=null){
            params.put("businessType",businessType.getValue());
        }
        if(StringUtils.isNotBlank(queryFlag)){
            params.put("queryFlag",queryFlag);
        }
        Page<CabinetCargoSpaceVO> list = cabinetCargoSpaceService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "柜子库位列表（柜子面板）", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "cabCode", value = "柜子编码", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/list4Cab")
    public PageResult<CabinetCargoSpaceVO> list4Cab(
            @RequestParam(name = "page",defaultValue = "1") Integer page
            , @RequestParam(name = "limit",defaultValue = "10") Integer limit
            , @RequestParam(name = "sort",defaultValue = "cast(t.code as unsigned)") String sort
            , @RequestParam(name = "order",defaultValue = "asc") String order
            , String cabCode
            , Long cabId
            , StorageInventorySpace.BusinessType businessType
            ,Integer emptyLattice
    ) {
        Page<CabinetCargoSpaceVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        params.put("emptyLattice", emptyLattice);
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        params.put("cabinetId",cabId);
        params.put("cabCode",cabCode);
        if (businessType!=null){
            params.put("businessType",businessType.getValue());
        }

        Page<CabinetCargoSpaceVO> list = cabinetCargoSpaceService.pageList4Cab(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "柜子库位列表（柜子面板,格子柜上新的格子）", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "cabCode", value = "柜子编码", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/list4Load")
    public PageResult<CabinetCargoSpaceVO> list4Load(
            @RequestParam(name = "page",defaultValue = "1") Integer page
            , @RequestParam(name = "limit",defaultValue = "10") Integer limit
            , @RequestParam(name = "sort",defaultValue = "cast(t.code as unsigned)") String sort
            , @RequestParam(name = "order",defaultValue = "asc") String order
            , String cabCode
            , Long cabId
            , StorageInventorySpace.BusinessType businessType
            ,String name
            ,Long productTypeId
            ,Long productCategoryId
            ,String queryFlag
    ) {
        Page<CabinetCargoSpaceVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        params.put("cabinetId",cabId);
        params.put("cabCode",cabCode);
        if (businessType!=null){
            params.put("businessType",businessType.getValue());
        }
        params.put("productTypeId",productTypeId);
        params.put("productCategoryId",productCategoryId);
        params.put("name",name);
        params.put("queryFlag",queryFlag);
        Page<CabinetCargoSpaceVO> list = cabinetCargoSpaceService.pageList4Load(pg, params);
        List<CellSingleInventoryProductVO> alarmList=cellSingleInventoryProductService.list4Alarm(new HashMap<>());
        Map alarmMap=new HashMap();
        alarmList.forEach(e->{
            alarmMap.put(e.getBelongCabinet()+"_"+e.getSkuCode(),e.getBelongCabinet()+e.getSkuCode());
        });
        list.getRecords().forEach(e->{
            if (alarmMap.containsKey(e.getMainCabCode()+"_"+e.getSkuCode())){
                e.setAlarm(true);
            }
        });
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "柜子库位列表（wx）", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "cabCode", value = "柜子编码", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/list4Wx")
    public PageResult<CabinetCargoSpaceVO> list4Wx(
            @RequestParam(name = "page",defaultValue = "1") Integer page
            , @RequestParam(name = "limit",defaultValue = "10") Integer limit
            , @RequestParam(name = "sort",defaultValue = "cast(t.code as unsigned)") String sort
            , @RequestParam(name = "order",defaultValue = "asc") String order
            , String cabCode
            , Long cabId
            , StorageInventorySpace.BusinessType businessType
            ,CabinetCargoSpace.SpaceNature spaceNature
    ) {
        Page<CabinetCargoSpaceVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        params.put("cabinetId", cabId);
        params.put("cabCode", cabCode);
        if (businessType != null) {
            params.put("businessType", businessType.getValue());
        }
        if (spaceNature!=null){
            params.put("spaceNature",spaceNature.getValue());
        }
        Page<CabinetCargoSpaceVO> list = cabinetCargoSpaceService.pageList4Cab(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "查询所有收刀柜货位(小程序)", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value = "编号", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/list4Applet")
    public PageResult<CabinetCargoSpaceVO> list4Applet(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "asc") String order
            , String code
            , String cabCode
            , Long cabinetId
            , String queryFlag
            , Cabinet.CabinetNature cabinetNature
            ,String mainCabCode
            ,String cabName
            ,CabinetCargoSpace.BusinessStatus businessStatus
            ,CabinetCargoSpace.SpaceNature spaceNature
    ){

        Page<CabinetCargoSpace> pg = new Page<>(page, limit);

        Map queryParam=new HashMap();
        queryParam.put("sort", StrUtil.toUnderlineCase(sort));
        queryParam.put("order",order);
        queryParam.put("code",code);
        queryParam.put("cabCode",cabCode);
        queryParam.put("cabName",cabName);
        queryParam.put("cabinetId",cabinetId);
        queryParam.put("queryFlag",queryFlag);
        queryParam.put("mainCabCode",mainCabCode);
        if (cabinetNature!=null){
            queryParam.put("cabinetNature",cabinetNature.getValue());
        }
        if(businessStatus != null){
            queryParam.put("businessStatus",businessStatus.getValue());
        }if(spaceNature != null){
            queryParam.put("spaceNature",spaceNature.getValue());
        }
        IPage<CabinetCargoSpaceVO> list = cabinetCargoSpaceService.pageList4pc(pg,queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "修改收刀柜货位(小程序)", notes = "修改")
    @PutMapping("/updateApplet")
    public JsonResult updateApplet(@Validated(CabinetCargoSpaceForm.Update.class) CabinetCargoSpaceForm cabinetCargoSpaceForm) {
        CabinetCargoSpace cabinetCargoSpace = cabinetCargoSpaceService.getById(cabinetCargoSpaceForm.getId());
        if(cabinetCargoSpace == null){
            return JsonResult.fail("修改失败！");
        }

        cabinetCargoSpace.setSpaceType(cabinetCargoSpaceForm.getSpaceType());
        cabinetCargoSpace.setSpaceProductType(cabinetCargoSpaceForm.getSpaceProductType());
        cabinetCargoSpace.setSkuCode(cabinetCargoSpaceForm.getSkuCode());
        cabinetCargoSpace.setPrincipal(cabinetCargoSpaceForm.getPrincipal());
        cabinetCargoSpace.setPrincipalName(cabinetCargoSpaceForm.getPrincipalName());
        cabinetCargoSpace.setBusinessStatus(cabinetCargoSpaceForm.getBusinessStatus());
        cabinetCargoSpace.setQuantity(cabinetCargoSpaceForm.getQuantity());
        cabinetCargoSpace.setRemark(cabinetCargoSpaceForm.getRemark());
        if (StringUtils.isNotBlank(cabinetCargoSpaceForm.getPrincipal())){
            cabinetCargoSpace.setPrincipal(","+cabinetCargoSpaceForm.getPrincipal()+",");
        }
        if (cabinetCargoSpaceService.updateById(cabinetCargoSpace)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }

    @ApiOperation(value = "获取盘验订单的柜子库位", notes = "搜索")
    @GetMapping("/getByCheckOrder")
    public JsonResult getByCheckOrder(Long orderId){
        Map paramMap=new HashMap();
        paramMap.put("inoutId",orderId);
        List<CabinetCargoSpaceVO> list=cabinetCargoSpaceService.list4app(paramMap);
        if (list.size()==0){
            return JsonResult.fail("未找到此订单的柜子库位");
        }
        return JsonResult.success(list.get(0));
    }

    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long cabinetCargoSpaceId) {
        CabinetCargoSpaceVO cabinetCargoSpaceVO = cabinetCargoSpaceService.get4appById(cabinetCargoSpaceId);
        if (cabinetCargoSpaceVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), cabinetCargoSpaceVO);
    }

    @PostMapping()
    public JsonResult add(@Validated CabinetCargoSpaceForm cabinetCargoSpaceForm) {
        if (cabinetCargoSpaceService.add(cabinetCargoSpaceForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

    @GetMapping("/getSpaceByCabCode")
    public JsonResult getSpaceByCabCode(String cabCode){
        return JsonResult.success(cabinetCargoSpaceService.getSpaceByCabCode(cabCode));
    }


}
